package LABS.lab7;

/**************************************************************
* Class SetTest
* @author Jeff Thomas - Thomas007
* @vesion 1.0.0
* Date: 04.01.2008
* Copyright 2008, Jeff Thomas
***************************************************************/
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import javax.swing.JOptionPane;

public class SetTest
{
	public static void main(String[] args)
	{
		Set<String> setA = new HashSet<String>();
		Set<String> setB = new HashSet<String>();
		System.out.println("Set A");
		setA.addAll(createSet());
		System.out.println("Set B");
		setB.addAll(createSet());
		execOper(chooseOper(), setA, setB);
	}
/**************************************************************
* Problem A
* @param setA - Set of Strings
* @param setB - Set of Strings
* Task:	1. Print union of sets A and B
* Side Effects: None
***************************************************************/
	public static void problemA(Set<String> setA, Set<String> setB)
	{
		Set<String> setC = new HashSet<String>();
		setA.addAll(setB);
		setC.addAll(setA);
		System.out.println("Union of A and B: " + setC.toString());
	}
/**************************************************************
* Problem B
* @param setA - Set of Strings
* @param setB - Set of Strings
* Task:	1. Print intersection of sets A and B
* Side Effects: None
***************************************************************/	
	public static void problemB(Set<String> setA, Set<String> setB)
	{
		Set<String> setC = new HashSet<String>();
		setA.retainAll(setB);
		setC.addAll(setA);
		System.out.println("Intersection of A and B: " + setC.toString());
	}
/**************************************************************
* Problem C
* @param setA - Set of Strings
* @param setB - Set of Strings
* Task:	1. Print difference of sets A and B
* Side Effects: None
***************************************************************/	
	public static void problemC(Set<String> setA, Set<String> setB)
	{
		Set<String> setC = new HashSet<String>();
		setA.removeAll(setB);
		setC.addAll(setA);
		System.out.println("Set A - Set B: " + setC.toString());
	}
/**************************************************************
* Problem D
* @param setA - Set of Strings
* @param setB - Set of Strings
* Task:	1. Determine of set A is a subset of B
*			2. If true, Set C is assigned set A
*			3. Else, Set c is assigned set B
* Side Effects: None
***************************************************************/	
	public static void problemD(Set<String> setA, Set<String> setB)
	{
		Set<String> setC = new HashSet<String>();
		if (setB.containsAll(setA))
		{
			setC.addAll(setA);
			System.out.println(setC.toString());
		}
		else
		{
			setC.addAll(setB);
			System.out.println(setC.toString());
		}
	}
/**************************************************************
* Create Set
* @return - Set of type String with our newly created Set
* Task:	1. Scan for user input of Strings
*			2. Add each input string to the set
*			3. Quit when the user enters 'Q'
* Side Effects: None
***************************************************************/
	public static Set<String> createSet()
	{
		Set<String> result = new HashSet<String>();
		Scanner in = new Scanner(System.in);
		boolean done = false;
      while (!done)
      {
         System.out.print("Add String to set, enter Q when done: ");
         String input = in.next();
         if (input.equalsIgnoreCase("Q")) 
            done = true;
         else
         {
            result.add(input);
         }
		}
		return result;
	}
/**************************************************************
* Choose Operation
* @return - Integer value of choice user made
* Task:	1. Create array with choices for the user about problem
*			2. Prevent the user for exiting the option box
* Side Effects: None
***************************************************************/	
	public static int chooseOper()
	{
		String[] levelChoices = {"Problem A", "Problem B", "Problem C", "Problem D"};
		int choice = JOptionPane.showOptionDialog(null, "Please select operation to perform on sets", 
				"Problem Selection", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
				null, levelChoices, levelChoices[0]);
		while (choice == JOptionPane.CLOSED_OPTION)
		{
			JOptionPane.showMessageDialog(null, "You did not select an operation!  Please select one.", 
					"Error", JOptionPane.ERROR_MESSAGE);
			choice = JOptionPane.showOptionDialog(null, "Please select operation to perform on sets", 
					"Problem Selection", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, 
					null, levelChoices, levelChoices[0]);
		}
		return choice;
	}
/**************************************************************
* Execute Operation
* @param choice - Integer of choice user made for problem to execute
* @param setA - Set of Strings
* @param setB - Set of Strings
* Task:	1. Perform the correct problem method based off user choice
* Side Effects: None
***************************************************************/	
	public static void execOper(int choice, Set<String> setA, Set<String> setB)
	{
		switch(choice) 
		{
			case 0: problemA(setA, setB); break;
			case 1: problemB(setA, setB); break;
			case 2: problemC(setA, setB); break;
			case 3: problemD(setA, setB); break;
		}
	}
}
/**************************************************************
* I, Jeff Thomas, certify that each part of this program was
* written by me, except as noted within the code.
*
*						_________________________
***************************************************************/